#include "ADC.h"


/* Use ADC1 */
void adc_init(void)
{
	GPIO_InitTypeDef myGPIO_adc1;
	ADC_InitTypeDef myADC_struct;	
	NVIC_InitTypeDef myNVIC_ADC1;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_OCInitTypeDef TIM_OCInitStructure;
	NVIC_InitTypeDef myNVIC_TIM3;
	uint16_t TimerPeriod = 0;
	uint16_t Channel1Pulse = 0;
	
	//Enable the ADC clock
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	//Enable the clock for the ADC GPIOs
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 
	
	//Configure the ADC input (PA0) in analog mode
	myGPIO_adc1.GPIO_Pin=GPIO_Pin_0; 
	myGPIO_adc1.GPIO_Mode=GPIO_Mode_AN;
	myGPIO_adc1.GPIO_Speed=GPIO_Speed_Level_3;
	myGPIO_adc1.GPIO_OType=GPIO_OType_PP;
	myGPIO_adc1.GPIO_PuPd=GPIO_PuPd_NOPULL;
	
	GPIO_Init(GPIOA, &myGPIO_adc1);
	
	//Configure the ADC input (PA3) in analog mode
	myGPIO_adc1.GPIO_Pin=GPIO_Pin_3; 
	GPIO_Init(GPIOA, &myGPIO_adc1);
	
	//Configure
	myADC_struct.ADC_Resolution=ADC_Resolution_12b;
	myADC_struct.ADC_ContinuousConvMode=ENABLE;	
	myADC_struct.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_RisingFalling;
	myADC_struct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_T3_TRGO;	// Use TIM3 to trigger ADC1 conversion
	myADC_struct.ADC_DataAlign=ADC_DataAlign_Right;
	myADC_struct.ADC_ScanDirection=ADC_ScanDirection_Upward;
	ADC_Init(ADC1, &myADC_struct);

	// configure ADC channels
	ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_13_5Cycles);
	
	// Configure the ADC interrupts
	ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
	
		//Enable the ADC
	ADC_Cmd(ADC1, ENABLE);
	
		// Enable the discontinuous mode
	ADC_DiscModeCmd(ADC1, ENABLE);
	
	//Enable the ADC interrupt vector 
	myNVIC_ADC1.NVIC_IRQChannel = ADC1_COMP_IRQn ;
	myNVIC_ADC1.NVIC_IRQChannelPriority = 3;
	myNVIC_ADC1.NVIC_IRQChannelCmd=ENABLE;

	// Interrupt priority  NVIC_PriorityGroupConfig()
	NVIC_Init(&myNVIC_ADC1);
	
/*-------------------------------------------------------*/	
	// Configure TIM3
	TimerPeriod = (SystemCoreClock / 10000 ) - 1;

  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);
  
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 0x2000;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);	// Choose output trigger source
	TIM_UpdateRequestConfig(TIM3, TIM_UpdateSource_Regular);
	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
	
 	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
 	//TIM_GenerateEvent(TIM3, TIM_EventSource_Update);
//   /* TIM3 counter enable */
  TIM_Cmd(TIM3, ENABLE);
	
	  /* TIM3 Main Output Enable */
  //TIM_CtrlPWMOutputs(TIM3, ENABLE);
	//Enable the ADC interrupt vector 
	myNVIC_TIM3.NVIC_IRQChannel = TIM3_IRQn ;
	myNVIC_TIM3.NVIC_IRQChannelPriority = 3;
	myNVIC_TIM3.NVIC_IRQChannelCmd=ENABLE;

	// Interrupt priority  NVIC_PriorityGroupConfig()
	NVIC_Init(&myNVIC_TIM3);
}

